package com.freemalll.system.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freemalll.common.core.utils.poi.ExcelUtil;
import com.freemalll.common.core.web.controller.BaseController;
import com.freemalll.common.core.web.domain.AjaxResult;
import com.freemalll.common.core.web.page.TableDataInfo;
import com.freemalll.common.log.annotation.Log;
import com.freemalll.common.log.enums.BusinessType;
import com.freemalll.common.security.annotation.RequiresPermissions;
import com.freemalll.system.domain.SysAccountLeader;
import com.freemalll.system.service.ISysAccountLeaderService;

/**
 * 账户领导关系 信息操作处理
 * 
 * @author freemalll
 */
@RestController
@RequestMapping("/accountLeader")
public class SysAccountLeaderController extends BaseController
{
    @Autowired
    private ISysAccountLeaderService accountLeaderService;

    /**
     * 获取账户领导关系列表
     */
    @RequiresPermissions("system:accountLeader:list")
    @GetMapping("/list")
    public TableDataInfo list(SysAccountLeader accountLeader)
    {
        startPage();
        List<SysAccountLeader> list = accountLeaderService.selectAccountLeaderList(accountLeader);
        return getDataTable(list);
    }

    @Log(title = "账户领导关系", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:accountLeader:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAccountLeader accountLeader)
    {
        List<SysAccountLeader> list = accountLeaderService.selectAccountLeaderList(accountLeader);
        ExcelUtil<SysAccountLeader> util = new ExcelUtil<SysAccountLeader>(SysAccountLeader.class);
        util.exportExcel(response, list, "账户领导关系数据");
    }

    /**
     * 根据账户领导关系编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        return success(accountLeaderService.selectAccountLeaderById(id));
    }

    /**
     * 根据账户ID获取领导关系
     */
    @GetMapping(value = "/account/{accountId}")
    public AjaxResult getInfoByAccountId(@PathVariable String accountId)
    {
        return success(accountLeaderService.selectAccountLeaderByAccountId(accountId));
    }

    /**
     * 根据领导账户ID获取下属关系
     */
    @GetMapping(value = "/leader/{leaderAccountId}")
    public AjaxResult getInfoByLeaderId(@PathVariable String leaderAccountId)
    {
        return success(accountLeaderService.selectAccountLeaderByLeaderId(leaderAccountId));
    }

    /**
     * 新增账户领导关系
     */
    @RequiresPermissions("system:accountLeader:add")
    @Log(title = "账户领导关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysAccountLeader accountLeader)
    {
        return toAjax(accountLeaderService.insertAccountLeader(accountLeader));
    }

    /**
     * 修改账户领导关系
     */
    @RequiresPermissions("system:accountLeader:edit")
    @Log(title = "账户领导关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysAccountLeader accountLeader)
    {
        return toAjax(accountLeaderService.updateAccountLeader(accountLeader));
    }

    /**
     * 删除账户领导关系
     */
    @RequiresPermissions("system:accountLeader:remove")
    @Log(title = "账户领导关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        accountLeaderService.deleteAccountLeaderByIds(ids);
        return success();
    }

    /**
     * 根据账户ID删除所有相关关系
     */
    @RequiresPermissions("system:accountLeader:remove")
    @Log(title = "账户领导关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/account/{accountId}")
    public AjaxResult removeByAccountId(@PathVariable String accountId)
    {
        return toAjax(accountLeaderService.deleteAccountLeaderByAccountId(accountId));
    }

    /**
     * 根据领导账户ID删除所有相关关系
     */
    @RequiresPermissions("system:accountLeader:remove")
    @Log(title = "账户领导关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/leader/{leaderAccountId}")
    public AjaxResult removeByLeaderId(@PathVariable String leaderAccountId)
    {
        return toAjax(accountLeaderService.deleteAccountLeaderByLeaderId(leaderAccountId));
    }

    /**
     * 批量添加账户领导关系
     */
    @RequiresPermissions("system:accountLeader:add")
    @Log(title = "账户领导关系", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchAdd(@Validated @RequestBody List<SysAccountLeader> accountLeaderList)
    {
        return toAjax(accountLeaderService.batchInsertAccountLeader(accountLeaderList));
    }
}